package com.snail.readyvr.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.adapter.CommonRecyclerAdapter;
import com.snail.readyvr.adapter.RecyclerViewHolder;
import com.snail.readyvr.glide.GlideUtils;
import com.snail.readyvr.ui.RoundedImageView;
import com.snail.readyvr.util.DialogUtils;
import com.snail.readyvr.util.LineItemDecoration;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.Utils;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import party.loveit.download.core.DLInfo;
import party.loveit.download.core.DLListenerMgr;
import party.loveit.download.core.DLManager;
import party.loveit.download.listener.IDListener;
import party.loveit.swipemenu.core.SwipeHorizontalMenuLayout;
import party.loveit.swipemenu.core.SwipeMenuRecyclerView;

/**
 * Created by MRKING on 2017/1/9.
 */

public class DownloadedFragment extends BaseFragment implements IDListener{
    @BindView(R.id.download_recycler_view)
    SwipeMenuRecyclerView mDownloadRecyclerView;
    @BindView(R.id.no_download_tip)
    TextView mNoDownloadTip;

    private CommonRecyclerAdapter<DLInfo> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_download;
    }

    @Override
    protected void init() {
        DLListenerMgr.getInstance().register(this);
        mNoDownloadTip.setText(R.string.no_video_downloaded_tip);
        mAdapter = new CommonRecyclerAdapter<DLInfo>(getActivity(), R.layout.item_downloaded) {
            @Override
            public void bindData(RecyclerViewHolder holder, final int position, final DLInfo item) {
                if(holder == null || item == null)
                    return;
                RoundedImageView roundedImageView = holder.getView(R.id.downloaded_img);
                GlideUtils.loadRoundImg(getActivity(), item.imgUrl, roundedImageView);
                holder.setText(R.id.downloaded_video_name, item.videoName)
                        .setText(R.id.downloaded_video_length, Utils.formatVideoLength(item.totalBytes))
                        .setText(R.id.downloaded_video_time, item.videoTime);
                final SwipeHorizontalMenuLayout swipeHorizontalMenuLayout = holder.getView(R.id.downloaded_swipe_menu);
                swipeHorizontalMenuLayout.smoothCloseMenu();
                holder.setOnClickListener(R.id.smMenuViewRight, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        swipeHorizontalMenuLayout.smoothCloseMenu();
                        //ToastUtils.show(getActivity(),"" + position);
                        DialogUtils.showDownloadDialog(getActivity(), getString(R.string.download_dialog_title), getString(R.string.download_dialog_content), null, getString(R.string.download_dialog_delete), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mAdapter.delete(position);
                                DLManager.getInstance().dlCancel(item.baseUrl);
                            }
                        });
                    }
                });
            }
        };
        mAdapter.setOnClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
              //跳转视频播放
                if(position < mAdapter.getItemCount())
                 StartActivityUtils.startVideoPlayer(getActivity(), mAdapter.getDataByPosition(position).dirPath + mAdapter.getDataByPosition(position).fileName);
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mDownloadRecyclerView.setLayoutManager(manager);
        mDownloadRecyclerView.addItemDecoration(new LineItemDecoration(getActivity(), LineItemDecoration.VERTICAL_LIST));
        mDownloadRecyclerView.setAdapter(mAdapter);

        getData();
    }

    private void getData() {

       List<DLInfo> infos = DLManager.getInstance().getAllCompleteDLInfo();
        if(infos != null && infos.size() > 0)
            mAdapter.appendList(infos);
        else
           mNoDownloadTip.setVisibility(View.VISIBLE);
        /*for(long i = 0; i<5;i++ ){
            DLInfo info = new DLInfo();
            info.videoName = "aaa" + i;
            info.imgUrl = "" + i;

            info.totalBytes = 1024 * 1024 *1024 * (i + 1);
            info.videoTime = "00:25:0" + i;
            mAdapter.add((int) i,info);

        }*/
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        DLListenerMgr.getInstance().unregister(this);
    }

    @Override
    public void onPrepare(String baseUrl) {

    }

    @Override
    public void onStart(String fileName, String baseUrl, long fileLength) {

    }

    @Override
    public void onProgress(String baseUrl, String speed, long progress) {

    }

    @Override
    public void onStop(String baseUrl, long progress) {

    }

    @Override
    public void onFinish(String baseUrl, File file) {

        DLInfo info = DLManager.getInstance().getDLInfo(baseUrl);
        if(mAdapter != null)
            mAdapter.add(mAdapter.getItemCount(), info);
        if(mNoDownloadTip != null && mNoDownloadTip.getVisibility() == View.VISIBLE)
            mNoDownloadTip.setVisibility(View.GONE);
    }

    @Override
    public void onError(String baseUrl, int status, String error) {

    }
}
